#include "key_process.h"
#include "string.h"
#include "delay.h"


key_para_t key_para;

void Key_Msg_Init(void)
{
    Key_Init();
    memset(&key_para, 0, sizeof(key_para_t));
    key_para.send_value = -1;
    // Delay_Ms(100);
}

#if 0       //常规按键

static int8_t Msg_K1_Handle(void)
{
    if (key_para.cur_page == PAGE1) {
        key_para.cur_page = PAGE2;
        key_para.send_value = KEY_IMAGE;
    } else if (key_para.cur_page == PAGE2)  {
        key_para.cur_page = PAGE1;
        key_para.send_value = KEY_SCREEN1_RET;
    }
}

static int8_t Msg_K2_Handle(void)
{
    if (key_para.cur_page == PAGE1) {
        key_para.cur_page = PAGE3;
        key_para.send_value = KEY_LOAD;
    } else if (key_para.cur_page == PAGE3)  {
        key_para.cur_page = PAGE1;
        key_para.send_value = KEY_SCREEN2_RET;
    }
}
static int8_t Msg_K3_Handle(void)
{
    key_para.send_value = KEY_VOL_ADD;
}

static int8_t Msg_K4_Handle(void)
{
    key_para.send_value = KEY_VOL_SUB;
}

#else //选项框

#define LV_NOT_VAL      0
#define LV_UP_VAL       1
#define LV_DOWN_VAL     2
#define LV_LEFT_VAL     3
#define LV_RIGHT_VAL    4
#define LV_ENTER_VAL    5
#define LV_EXIT_VAL     6


static uint32_t Msg_K1_Handle(void)
{
    return LV_UP_VAL;
	
}

static uint32_t Msg_K2_Handle(void)
{
    return LV_DOWN_VAL;
}
static uint32_t Msg_K3_Handle(void)
{
    return LV_LEFT_VAL;
}

static uint32_t Msg_K4_Handle(void)
{
    return LV_RIGHT_VAL;
}

static uint32_t Msg_K5_Handle(void)
{
    return LV_ENTER_VAL;
}

static uint32_t Msg_K6_Handle(void)
{
    return LV_EXIT_VAL;
}

#endif

const key_func_t key_func[] = {
    {MSG_K1_PRESS, Msg_K1_Handle},
    {MSG_K2_PRESS, Msg_K2_Handle},
    {MSG_K3_PRESS, Msg_K3_Handle},
    {MSG_K4_PRESS, Msg_K4_Handle},
    {MSG_K5_PRESS, Msg_K5_Handle},
    {MSG_K6_PRESS, Msg_K6_Handle},
};


uint32_t Key_Event_Handle(uint16_t cmd)
{
    if (MSG_INVALID == cmd)
        return key_para.send_value;


    if (key_para.cur_value != cmd) {
        key_para.cur_value = cmd;
        switch (key_para.cur_value) {
            case 0 : {
                return LV_NOT_VAL;
                break;
            }

            default : {
                for (uint8_t i = 0; i < sizeof(key_func) / sizeof(key_func[0]); i++) {
                    if (key_para.cur_value == key_func[i].index) {
                        if (key_func[i].func) {
                            key_func[i].func();
                        }
                    }
                }
                break;
            }
        }
    }
}
